package com.macro.mall.service.impl;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dao.DeviceDao;
import com.macro.mall.dto.Device;
import com.macro.mall.dto.DeviceExample;
import com.macro.mall.dto.Device;
import com.macro.mall.dto.DeviceRequest;
import com.macro.mall.service.DeviceService;
import com.macro.mall.util.example.ExampleUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author HuangZiy
 * @version 8.0
 * @date 2021/4/26 22:43
 */
@Service
public class DeviceServiceImpl implements DeviceService {
    @Autowired
    private DeviceDao deviceDao;

    @Override
    public CommonResult<List<Device>> getDevice(DeviceRequest request) {
        DeviceExample deviceExample = ExampleUtil.getExampleByAnnotation(request, DeviceExample.class);
        List<Device> devices = deviceDao.selectByExample(deviceExample);
        if (CollectionUtils.isEmpty(devices)) {
            return CommonResult.success(null);
        }
        return CommonResult.success(devices);
    }

    @Override
    public CommonResult<String> addDevice(DeviceRequest request) {
        Device device = new Device();
        BeanUtils.copyProperties(request, device);
        int i = deviceDao.insertSelective(device);
        if (i <= 0) {
            return CommonResult.failed("插入失败");
        }
        return CommonResult.success("插入成功");
    }

    @Override
    public CommonResult<String> updateDevice(DeviceRequest request) {
        Device device = new Device();
        BeanUtils.copyProperties(request, device);
        int i = deviceDao.updateByPrimaryKeySelective(device);
        if (i <= 0) {
            return CommonResult.failed("修改失败");
        }
        return CommonResult.success("修改成功");
    }

    @Override
    public CommonResult<String> deleteDevice(Integer id) {
        int i = deviceDao.deleteByPrimaryKey(id);
        if (i <= 0) {
            return CommonResult.failed("删除失败");
        }
        return CommonResult.success("删除成功");
    }
}
